<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Training extends CI_Controller {
 	public function __construct()
    {
        parent::__construct();
        session_start();
        //$this->output->enable_profiler(FALSE);
    	if (!isset($_SESSION['uid']) || $_SESSION['uid'] < 1){
	    	redirect('memberlogin/requiredmessage','refresh');
	    }
	    elseif ($_SESSION['role_id'] !=1){
    		redirect('memberlogin/restriction','refresh');
	    }
	    $data['menus'] = 'menus';
	    $data['left'] = 'training/training_left_menu';
		$this->load->vars($data);
    }
	    
	public function index()
	{
	    $data['title'] = "Staff Training";
		$data['main'] = 'training/training';
		$data['profiles'] = $this->MStaff->getAllProfiles('');
		$this->load->vars($data);
		$this->load->view('template'); 	 
	}
    
    public function search()
    {
        if($this->input->post('searchtraining')){ 
            $data['title'] = "Staff Training";
    		$data['main'] = 'training/training';
    		$data['profiles'] = $this->MStaff->getAllProfiles($this->input->post('searchtraining'));
    		$this->load->vars($data);
    		$this->load->view('template');   
        }else {
           redirect('training','refresh'); 
        }
    }
    
    public function viewtraining($id)
    {
        $data['title'] = "Staff Training";
		$data['main'] = 'training/viewtraining';
        
        //receive training
        $url_edit_rectraining = "/index.php/training/edittraining/".$id."#tabs-1";
        $url_cancel = "/index.php/training";
 	    $data['formeditcancelrectraining'] = $this->MFormGlobal->getFormEditCancel($url_edit_rectraining,$url_cancel);
        $data['data_rectraining'] = $this->MTraining->getReceiveTrainingByStaffId($id);  
        
         //desirabletraining
        $url_edit_desirabletraining = "/index.php/training/edittraining/".$id."#tabs-2";
        $data['formeditcanceldesirabletraining'] = $this->MFormGlobal->getFormEditCancel($url_edit_desirabletraining,$url_cancel);
        $data['data_desirabletraining'] = $this->MTraining->getDesirableTrainingByStaffId($id);  
        
         //staffskill
        $url_edit_staffskill = "/index.php/training/edittraining/".$id."#tabs-3";
        $data['formeditcancelstaffskill'] = $this->MFormGlobal->getFormEditCancel($url_edit_staffskill,$url_cancel);
        $data['data_staffskill'] = $this->MTraining->getStaffSkillsByStaffId($id); 
        
        //stafffieldvisit
        $url_edit_stafffieldvisit = "/index.php/training/edittraining/".$id."#tabs-4";
        $data['formeditcancelstafffieldvisit'] = $this->MFormGlobal->getFormEditCancel($url_edit_stafffieldvisit,$url_cancel);
        $data['data_stafffieldvisit'] = $this->MTraining->getStaffFieldVisitByStaffId($id);   
        
        $this->load->vars($data);
		$this->load->view('template');
    }
    
    public function edittraining($id)
    {
        $data['title'] = "Staff Training";
		$data['main'] = 'training/edittraining';
        $data['id']=$id;
        //receive training
        $url_cancel_rectraining = "/index.php/training/viewtraining/".$id."#tabs-1";
 	    $data['formsavecancelrectraining'] = $this->MFormGlobal->getFormSaveCancel($url_cancel_rectraining);
		
        $data['course'] = $this->MTraining->getCourseOption();
        $data['RTType'] = $this->MTraining->geRecTrainingTypeOption();
        $data['PlaningTime'] = $this->MTraining->getPlaningTimeOption();
        $query = "SELECT sr.`RTID`,sr.`StaffID`, sr.`RTType`, sr.`DateRTraining`,qt.`QPID`, c.`CourseID`, c.`Topic`, sr.`TrainingClass`, sr.`Duration`,sr.`Comment`  
                    FROM `staffrectraining` AS sr
                    INNER JOIN `staff` AS s ON s.`StaffID` = sr.`StaffID`
                    LEFT JOIN `quaterplan` AS qt ON qt.`QPID` = sr.`PlaningTime`
                    INNER JOIN `course` AS c ON c.`CourseID` = sr.`CourseID`
                    where sr.`StaffID`=".$id;
        $result =  $this->db->query($query);
        $data['data_rectraining'] = $result;  
        
        if(isset($_POST)){
    		$post = $_POST;
    		if(isset($post['counter_rectraining'])){
    		    $deleteQuery = "DELETE FROM staffrectraining where StaffID=".$id;
                $this->db->query($deleteQuery);
          	 
    			for ($i = 1; $i <= $post['counter_rectraining']; $i++ ){
	    			$data = array(
	    					'DateRTraining' => $post['DateRTraining_' . $i],
	    					'CourseID' => $post['CourseID_'. $i],
	    					'RTType' =>$post['RTType_'. $i],
	    					'PlaningTime' => $post['PlaningTime_'. $i],
	    					'TrainingClass' => $post['TrainingClass_'. $i],
	    					'Duration'=> $post['Duration_'. $i],
                            'Comment'=>$post['Comment_'.$i],
	    					'StaffID' => $id,
    				);
    				$this->db->insert('staffrectraining', $data);
    			}
                redirect('/training/viewtraining/'.$id.'#tabs-1','refresh');
    			exit();
    		}    		
    	} 
        
         //desirabletraining
        $url_cancel_desirabletraining = "/index.php/training/viewtraining/".$id."#tabs-2";
        $data['formsavecanceldesirabletraining'] = $this->MFormGlobal->getFormSaveCancel($url_cancel_desirabletraining);
        $query = "SELECT sd.`ID`, sd.`StaffID`, c.`Topic`, c.`CourseID`,sd.`Comment`, sd.`DateTraining` FROM `staffdeserabletraining` AS sd
                    INNER JOIN staff AS s ON s.`StaffID` = sd.`StaffID`
                    INNER JOIN `course` AS c ON c.`CourseID` = sd.`CourseID`
                    where  sd.`StaffID`=".$id;
        $result =  $this->db->query($query);
        $data['data_desirabletraining'] = $result;  
        
        if(isset($_POST)){
    		$post = $_POST;
    		if(isset($post['counter_desirabletraining'])){
    		    $deleteQuery = "DELETE FROM staffdeserabletraining where StaffID=".$id;
                $this->db->query($deleteQuery);
    			
    			for ($i = 1; $i <= $post['counter_desirabletraining']; $i++ ){
	    			$data = array(
	    					'DateTraining' => $post['DateTraining_' . $i],
	    					'CourseID' => $post['CourseID_'. $i],
                            'Comment' =>$post['Comment_'.$i],
	    					'StaffID' => $id,
    				);
    				$this->db->insert('staffdeserabletraining', $data);
    			}
                redirect('/training/viewtraining/'.$id.'#tabs-2','refresh');
    			exit();
    		}    		
    	} 
        
         //staffskill
        $url_cancel_staffskill = "/index.php/training/viewtraining/".$id."#tabs-3";
        $data['formsavecancelstaffskill'] = $this->MFormGlobal->getFormSaveCancel($url_cancel_staffskill);
        $query = "SELECT ss.`StaffSkillID`, ss.`StaffID`, ss.`CourseID`, c.`Topic`, ss.`Remark` FROM `staffskill` AS ss
                INNER JOIN staff AS s ON s.`StaffID` = ss.`StaffID`
                INNER JOIN `course` AS c ON c.`CourseID` = ss.`CourseID`
                    where ss.`StaffID`=".$id;
        $result =  $this->db->query($query);
        $data['data_staffskill'] = $result; 
        
        if(isset($_POST)){
    		$post = $_POST;
    		if(isset($post['counter_staffskill'])){
    		    $deleteQuery = "DELETE FROM staffskill where StaffID=".$id;
                $this->db->query($deleteQuery);
    			
    			for ($i = 1; $i <= $post['counter_staffskill']; $i++ ){
	    			$data = array(
                            'CourseID' => $post['CourseID_'. $i],
	    					'Remark' => $post['Remark_' . $i],
	    					'StaffID' => $id,
    				);
    				$this->db->insert('staffskill', $data);
    			}
                redirect('/training/viewtraining/'.$id.'#tabs-3','refresh');
    			exit();
    		}    		
    	} 
        //stafffieldvisit
        $url_cancel_stafffieldvisit = "/index.php/training/viewtraining/".$id."#tabs-4";
        $data['formsavecancelstafffieldvisit'] = $this->MFormGlobal->getFormSaveCancel($url_cancel_stafffieldvisit);        
        $data['location'] = $this->MTraining->getLocationOption();
        $data['visittype'] = $this->MTraining->getVisitTypeOption();
        $query = "SELECT sf.`FieldVisitID`, sf.`StaffID`, sf.`VisitType`, l.`LocationID`, l.`Location`, sf.`DateFieldVisit`, qp.`QPID`, qp.`QuaterPlan`, sf.`Duration`, sf.`Purpose`, sf.`Comment` FROM `stafffieldvisit` AS sf
                INNER JOIN staff AS s ON s.`StaffID` = sf.`StaffID`
                INNER JOIN location AS l ON l.`LocationID` = sf.`LocationID`
                LEFT JOIN quaterplan AS qp ON qp.`QPID` = sf.`PlaningTime`
                    where sf.`StaffID`=".$id;
        $result =  $this->db->query($query);
        $data['data_stafffieldvisit'] = $result;
        if(isset($_POST)){
    		$post = $_POST;
    		if(isset($post['counter_stafffieldvisit'])){
    		    $deleteQuery = "DELETE FROM stafffieldvisit where StaffID=".$id;
                $this->db->query($deleteQuery);
    			
    			for ($i = 1; $i <= $post['counter_stafffieldvisit']; $i++ ){
	    			$data = array(
                            'DateFieldVisit' => $post['DateFieldVisit_'. $i],
	    					'LocationID' => $post['LocationID_' . $i],
                            'VisitType'=> $post['VisitType_' . $i],
                            'PlaningTime'=> $post['PlaningTime_' . $i],
                            'Duration'=> $post['Duration_' . $i],
                            'Purpose'=> $post['Purpose_' . $i],
                            'Comment'=> $post['Comment_' . $i],
	    					'StaffID' => $id,
    				);
    				$this->db->insert('stafffieldvisit', $data);
    			}
                redirect('/training/viewtraining/'.$id.'#tabs-4','refresh');
    			exit();
    		}    		
    	} 
        $this->load->vars($data);
		$this->load->view('template');        
    }
    
    public function trainingclass()
    {
        $data['title'] = "Training Class";
		$data['main'] = 'training/trainingclass';
        $url_add ="addtrainingclass";
        $data['formaddtrainingclass']= $this->MFormGlobal->getFormAdd($url_add);
       
        $query = "SELECT tc.`TCID`, c.`CourseID`, c.`Topic`,tc.`TCDate`, tc.`Duration`, tr.`TRID`, tr.`TrainingRoom`, tc.`ParticipantM`, tc.`ParticipantF`, tc.`Evaluation`, tc.`Remark`
                    FROM `trainingclass` AS tc
                    INNER JOIN `course` AS c ON c.`CourseID` = tc.`CourseID`
                    INNER JOIN `trainingroom` AS tr ON tr.`TRID` = tc.`TrainingRoom`
                    ORDER BY tc.`TCDate` DESC";
        $result =  $this->db->query($query);
        $data['data'] = $result;  
		$this->load->vars($data);
		$this->load->view('template');
    }
    
    public function viewtrainingclass($id)
    {
        $data['title'] = "View Training Class";
		$data['main'] = 'training/viewtrainingclass';
        $url_edit = '/index.php/training/edittrainingclass/'.$id;
        $url_cancel ="/index.php/training/trainingclass";
        $data['formedittrainingclass']= $this->MFormGlobal->getFormEditCancel($url_edit,$url_cancel);
        
        $query = "SELECT tc.`TCID`, c.`CourseID`, c.`Topic`,tc.`TCDate`, tc.`Duration`, tr.`TRID`, tr.`TrainingRoom`, tc.`ParticipantM`, tc.`ParticipantF`, tc.`Evaluation`, tc.`Remark`
                    FROM `trainingclass` AS tc
                    INNER JOIN `course` AS c ON c.`CourseID` = tc.`CourseID`
                    INNER JOIN `trainingroom` AS tr ON tr.`TRID` = tc.`TrainingRoom`
                    where tc.`TCID`=".$id;
        $result =  $this->db->query($query);
        $result = $result ->row();
        $data['row'] = $result;  
		$this->load->vars($data);
		$this->load->view('template'); 
    }
    
     public function addtrainingclass()
    {
        if (isset($_POST) && $_POST !=null){
	  		$this->MTraining->addtrainingclass($_POST);	
	  		$this->session->set_flashdata('message',' Training class created');
			redirect('training/trainingclass','refresh');
	  	}
        $data['title'] = "Add Training Class";
		$data['main'] = 'training/addtrainingclass';
        $url_cancel ="trainingclass";
        $data['formsavetrainingclass']= $this->MFormGlobal->getFormSaveCancel($url_cancel);
        
        $queryTopic = "SELECT `CourseID`, `Topic` FROM `course`";
        $result = $this->db->query($queryTopic);
        $data['topic'] = $result;
        
        $queryTrainingRoom = "SELECT `TRID`, `TrainingRoom` FROM `trainingroom`";
        $resultTrainingRoom = $this->db->query($queryTrainingRoom);
        $data['trainingroom'] = $resultTrainingRoom;
        
		$this->load->vars($data);
		$this->load->view('template'); 
    }
    
    public function edittrainingclass($id)
    {
        if (isset($_POST) && $_POST !=null){
	  		$this->MTraining->edittrainingclass($_POST);	
	  		$this->session->set_flashdata('message',' Training Class updated');
			redirect('training/viewtrainingclass/'.$id,'refresh');
	  	}
        $data['title'] = "Edit Training Class";
		$data['main'] = 'training/edittrainingclass';
        $url_cancel ="/index.php/training/trainingclass";
        $data['formsavetrainingclass']= $this->MFormGlobal->getFormSaveCancel($url_cancel);
        
        $queryTopic = "SELECT `CourseID`, `Topic` FROM `course`";
        $result = $this->db->query($queryTopic);
        $data['topic'] = $result;
        
        $queryTrainingRoom = "SELECT `TRID`, `TrainingRoom` FROM `trainingroom`";
        $resultTrainingRoom = $this->db->query($queryTrainingRoom);
        $data['trainingroom'] = $resultTrainingRoom;
        
        $query = "SELECT tc.`TCID`, c.`CourseID`, c.`Topic`,tc.`TCDate`, tc.`Duration`, tr.`TRID`, tr.`TrainingRoom`, tc.`ParticipantM`, tc.`ParticipantF`, tc.`Evaluation`, tc.`Remark`
                    FROM `trainingclass` AS tc
                    INNER JOIN `course` AS c ON c.`CourseID` = tc.`CourseID`
                    INNER JOIN `trainingroom` AS tr ON tr.`TRID` = tc.`TrainingRoom`
                    where tc.`TCID`=".$id;
        $result =  $this->db->query($query);
        $result = $result ->row();
        $data['data'] = $result;  
        
		$this->load->vars($data);
		$this->load->view('template'); 
     }
    
    public function touchbase()
    {
        $data['title'] = "Touch Base";
		$data['main'] = 'training/touchbase';
        $url_add ="addtouchbase";
        $data['formaddtouchbase']= $this->MFormGlobal->getFormAdd($url_add);
       
        $query = "SELECT t.`TouchBaseID`, t.`DateTouchBase`, l.`Location`,s.`FirstName`, s.`LastName`, t.`Course`, t.`Duration`, t.`Purpose`, t.`Comment`
                    FROM `touchbase` AS t 
                    INNER JOIN `location` AS l ON t.`Location` = l.`LocationID`
                    INNER JOIN staff AS s ON t.`StaffID` = s.`StaffID`
                    ORDER BY t.`DateTouchBase` DESC";
        $result =  $this->db->query($query);
        $data['data'] = $result;  
		$this->load->vars($data);
		$this->load->view('template');
    }
    
    public function viewtouchbase($TouchBaseID)
    {
        $data['title'] = "View Touch Base";
		$data['main'] = 'training/viewtouchbase';
        $url_edit = '/index.php/training/edittouchbase/'.$TouchBaseID;
        $url_cancel ="/index.php/training/touchbase";
        $data['formedittouchbase']= $this->MFormGlobal->getFormEditCancel($url_edit,$url_cancel);
        
        $query = "SELECT t.`TouchBaseID`, t.`DateTouchBase`, l.`Location`,CONCAT(s.`FirstName`,' ', s.`LastName`) as staffname, t.`Course`, t.`Duration`, t.`Purpose`, t.`Comment`
                    FROM `touchbase` AS t 
                    INNER JOIN `location` AS l ON t.`Location` = l.`LocationID`
                    INNER JOIN staff AS s ON t.`StaffID` = s.`StaffID`
                    where t.`TouchBaseID`=".$TouchBaseID;
        $result =  $this->db->query($query);
        $result = $result ->row();
        $data['row'] = $result;  
		$this->load->vars($data);
		$this->load->view('template'); 
    }
    
    public function addtouchbase()
    {
        if (isset($_POST) && $_POST !=null){
	  		$this->MTraining->addtouchbase($_POST);	
	  		$this->session->set_flashdata('message',' Touchbase created');
			redirect('training/touchbase','refresh');
	  	}
        $data['title'] = "Add Touch Base";
		$data['main'] = 'training/addtouchbase';
        $url_cancel ="touchbase";
        $data['formsavetouchbase']= $this->MFormGlobal->getFormSaveCancel($url_cancel);
        
        $queryStaff = "SELECT `StaffID`, CONCAT(`FirstName` ,' ', `LastName`) AS staffname FROM `staff`";
        $result = $this->db->query($queryStaff);
        $data['staffname'] = $result;
        
        $queryLocation = "SELECT `LocationID`, `Location` FROM `location`";
        $resultLocation = $this->db->query($queryLocation);
        $data['location'] = $resultLocation;
        
		$this->load->vars($data);
		$this->load->view('template'); 
    }
    
    public function edittouchbase($TouchBaseID)
    {
        if (isset($_POST) && $_POST !=null){
	  		$this->MTraining->edittouchbase($_POST);	
	  		$this->session->set_flashdata('message',' Touchbase updated');
			redirect('training/viewtouchbase/'.$TouchBaseID,'refresh');
	  	}
        $data['title'] = "Edit Touch Base";
		$data['main'] = 'training/edittouchbase';
        $url_cancel ="/index.php/training/touchbase";
        $data['formsavetouchbase']= $this->MFormGlobal->getFormSaveCancel($url_cancel);
        
        $queryStaff = "SELECT `StaffID`, CONCAT(`FirstName` ,' ', `LastName`) AS staffname FROM `staff`";
        $result = $this->db->query($queryStaff);
        $data['staffname'] = $result;
        
        $queryLocation = "SELECT `LocationID`, `Location` FROM `location`";
        $resultLocation = $this->db->query($queryLocation);
        $data['location'] = $resultLocation;
        
        $query = "SELECT t.`TouchBaseID`, t.`DateTouchBase`,l.`LocationID`, l.`Location`,s.`StaffID`,CONCAT(s.`FirstName`,' ', s.`LastName`) as staffname, t.`Course`, t.`Duration`, t.`Purpose`, t.`Comment`
                    FROM `touchbase` AS t 
                    INNER JOIN `location` AS l ON t.`Location` = l.`LocationID`
                    INNER JOIN staff AS s ON t.`StaffID` = s.`StaffID`
                    where t.`TouchBaseID`=".$TouchBaseID;
        $result =  $this->db->query($query);
        $result = $result ->row();
        $data['data'] = $result;  
        
		$this->load->vars($data);
		$this->load->view('template'); 
     }
}
